import shop from '@/assets/shopAPI.js'
export default {
    namespaced: true,
    state: {
        list: [],
        checkoutStatus: null
    },
    mutations: {
        setList(state, payload) {
            state.list = payload
        },
        // push方法
        pushProductToCart(state, { id }) {
            state.list.push({ id, quantity: 1 })
        },
        // quantity+1
        incItemQuantity(state, { id }) {
            const cartItem = state.list.find(item => item.id === id)
            cartItem.quantity++
        },
        // quantity-1
        decItemQuantity(state, { id }) {
            const cartItem = state.list.find(item => item.id === id)
            cartItem.quantity--
        },
        // 结算状态判断
        setCheckoutStatus(state, status) {
            state.checkoutStatus = status
        }
    },
    actions: {
        remove({ commit, state }, product) {
            const cartItem = state.list.find(item => item.id === product.id)
            commit('decItemQuantity', cartItem)
            commit('pro/incProductInventory', { id: product.id }, { root: true })
        },
        addProToCart({ commit, state }, product) {
            // 新增购物车时，结算状态情清空
            commit('setCheckoutStatus', null)
            if (product.inventory > 0) {
                const cartItem = state.list.find(item => item.id === product.id)
                //如果购物车里没有该项，push，如果有，找到对应ID，quantity数量+1
                if (!cartItem) {
                    commit('pushProductToCart', { id: product.id })
                } else {
                    commit('incItemQuantity', cartItem)
                }
                // 库存-1
                commit('pro/decProductInventory', { id: product.id }, { root: true })
            }
        },
        // 购物车结账
        checkout({ commit, state }, products) {
            // 储存购物车清单备份（结算失败时返回该值）
            const savedCartList = [...state.list]
            // 不论上次结算状态如何，清空状态，重新结算
            commit('setCheckoutStatus', null)
            // 情况购物车
            commit('setList', [])
            // 调用API里的购买函数，传入3个参数，后两个参数为函数
            // 对应结算成功和结算失败，结算后修改结算状态
            shop.buyProducts(
                products,
                () => commit('setCheckoutStatus', '结算成功'),
                () => {
                    commit('setCheckoutStatus', '结算失败，请检查网络连接后再试一次')
                    // 结算失败后返回原购物车清单
                    commit('setList', savedCartList)
                }
            )
        },
    },
    getters: {
        // 购物车里的商品展示
        cartPro: (state, getters, rootState) => {
            return state.list.map(({ id, quantity }) => {
                const product = rootState.pro.list.find(product => product.id === id)
                return {
                    id: product.id,
                    title: product.title,
                    price: product.price,
                    inventory: product.inventory,
                    quantity
                }
            })
        },
        cartProFilter: (state, getters) => {
            return getters.cartPro.filter(item => item.quantity > 0)
        },
        // 购物车总价
        cartTotalPrice: (state, getters) => {
            return getters.cartPro.reduce((total, product) => {
                return total + product.price * product.quantity
            }, 0)
        }
    }
}